package interceptor;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

import utils.DateUtil;
import utils.StringUtil;
import views.VOperatorResource;

public class ResourceInterceptor implements Interceptor {

	private static final long serialVersionUID = -2578561479301489061L;
	private static final String TYPE_DATE = "DATE";
	private static final String TYPE_TEXT = "TEXT";
	private static final String TYPE_NUMBER = "NUMBER";

	public void init() {
		System.out.println("资源内容拦截器初始化");
	}

	public void destroy() {
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		String invocationAction = invocation.getAction().getClass().getName();
		String invocationMethod = invocation.getProxy().getMethod();

		@SuppressWarnings("unchecked")
		List<VOperatorResource> operatorResourceList = (List<VOperatorResource>) invocation.getInvocationContext().getSession().get("operatorResourceList");
		// 如果不属于资源管理范围则直接跳过
		if (operatorResourceList == null) {
			return invocation.invoke();
		}

		for (VOperatorResource vOperatorResource : operatorResourceList) {
			if (vOperatorResource.getResAction().equals(invocationAction) && vOperatorResource.getResMethod().equals(invocationMethod)) {
				Map<String, Object> parameters = invocation.getInvocationContext().getParameters();

				// 日期类型
				// 判断日期上限top是否为空,如果为空则直接用资源配置项的时间来填充日期上限,如果不为空则计算资源配置项的上限和输入的日期上限哪个靠前使用哪个
				// 判断日期下限end是否为空,如果为空则直接使用new date()填充,如果不为空则计算方式与top相反
				if (vOperatorResource.getResType().equals(TYPE_DATE)) {
					if (!parameters.containsKey(vOperatorResource.getResTarget() + "Top") || parameters.get(vOperatorResource.getResTarget() + "Top") == null
							|| ((String[]) parameters.get(vOperatorResource.getResTarget() + "Top")).length == 0 || StringUtil.isEmpty(((String[]) parameters.get(vOperatorResource.getResTarget() + "Top"))[0])) {
						parameters.put(vOperatorResource.getResTarget() + "Top", new Object[] { DateUtil.intervalDay(-new Integer(vOperatorResource.getResTerm())) });
					} else {
						String[] invocationKey = (String[]) parameters.get(vOperatorResource.getResTarget() + "Top");
						Date inpDate = new SimpleDateFormat("yyyy-MM-dd").parse(invocationKey[0]);
						Date resDate = DateUtil.intervalDay(-new Integer(vOperatorResource.getResTerm()));
						if (inpDate.getTime() > resDate.getTime()) {
							parameters.put(vOperatorResource.getResTarget() + "Top", inpDate);
						} else {
							parameters.put(vOperatorResource.getResTarget() + "Top", resDate);
						}
					}
					if (!parameters.containsKey(vOperatorResource.getResTarget() + "End") || parameters.get(vOperatorResource.getResTarget() + "End") == null
							|| ((String[]) parameters.get(vOperatorResource.getResTarget() + "End")).length == 0 || StringUtil.isEmpty(((String[]) parameters.get(vOperatorResource.getResTarget() + "End"))[0])) {
						parameters.put(vOperatorResource.getResTarget() + "End", new Object[] { new Date() });
					} else {
						String[] invocationKey = (String[]) parameters.get(vOperatorResource.getResTarget() + "End");
						Date inpDate = new SimpleDateFormat("yyyy-MM-dd").parse(invocationKey[0]);
						Date resDate = new Date();
						if (inpDate.getTime() < resDate.getTime()) {
							parameters.put(vOperatorResource.getResTarget() + "End", inpDate);
						} else {
							parameters.put(vOperatorResource.getResTarget() + "End", resDate);
						}
					}
				}

				// 数字类型
				// 判断数字上限top是否为空,如果为空则直接用资源配置项的值来填充参数上限,如果不为空则计算资源配置项的上限和输入的数值上限哪个靠前使用哪个
				// 判断数字下限end是否为空,如果为空则直接用资源配置项的值来填充参数下限,如果不为空则计算资源配置项的下限和输入的数值下限哪个靠后使用哪个
				if (vOperatorResource.getResType().equals(TYPE_NUMBER)) {
					if (!parameters.containsKey(vOperatorResource.getResTarget() + "Top") || parameters.get(vOperatorResource.getResTarget() + "Top") == null
							|| ((String[]) parameters.get(vOperatorResource.getResTarget() + "Top")).length == 0 || StringUtil.isEmpty(((String[]) parameters.get(vOperatorResource.getResTarget() + "Top"))[0])) {
						parameters.put(vOperatorResource.getResTarget() + "Top", new Object[] { new Double(vOperatorResource.getResTerm().split(",")[0]) });
					} else {
						String[] invocationKey = (String[]) parameters.get(vOperatorResource.getResTarget() + "Top");

						Double inpNumber = new Double(invocationKey[0]);
						Double resNumber = new Double(vOperatorResource.getResTerm().split(",")[0]);
						if (inpNumber > resNumber) {
							parameters.put(vOperatorResource.getResTarget() + "Top", inpNumber);
						} else {
							parameters.put(vOperatorResource.getResTarget() + "Top", resNumber);
						}
					}
					if (!parameters.containsKey(vOperatorResource.getResTarget() + "End") || parameters.get(vOperatorResource.getResTarget() + "End") == null
							|| ((String[]) parameters.get(vOperatorResource.getResTarget() + "End")).length == 0 || StringUtil.isEmpty(((String[]) parameters.get(vOperatorResource.getResTarget() + "End"))[0])) {
						parameters.put(vOperatorResource.getResTarget() + "End", new Object[] { new Double(vOperatorResource.getResTerm().split(",")[1]) });
					} else {
						String[] invocationKey = (String[]) parameters.get(vOperatorResource.getResTarget() + "End");
						Double inpNumber = new Double(invocationKey[0]);
						Double resNumber = new Double(vOperatorResource.getResTerm().split(",")[1]);
						if (inpNumber > resNumber) {
							parameters.put(vOperatorResource.getResTarget() + "End", resNumber);
						} else {
							parameters.put(vOperatorResource.getResTarget() + "End", inpNumber);
						}
					}
				}

				// 文本类型
				// 判断输入参数是否为空,如果为空则直接使用资源项的数值来填充查询参数值,例如1,2,3,4,5,资源限制1,2,则查询时直接用1,2填充查询参数
				// 如果输入参数不为空,则使用查询参数和资源项数据的重叠项填充查询参数,例如1,2,3,4,5
				// 查询1,3,则查询参数变为1,查询2,则查询参数变为2
				// 如果输入参数和资源项数据没有交集,例如1,2,3,4,5, 资源限制1,2,查询3,4
				// ,则视为查询参数为空,重新填充为资源项数据1,2作为查询参数
				if (vOperatorResource.getResType().equals(TYPE_TEXT)) {
					if (!parameters.containsKey(vOperatorResource.getResTarget()) || parameters.get(vOperatorResource.getResTarget()) == null || ((Object[]) parameters.get(vOperatorResource.getResTarget())).length == 0
							|| StringUtil.isEmpty(((Object[]) parameters.get(vOperatorResource.getResTarget()))[0])) {
						parameters.put(vOperatorResource.getResTarget(), new Object[] { vOperatorResource.getResTerm() });
					} else {
						StringBuffer sb = new StringBuffer();
						Object[] invocationKey = (Object[]) parameters.get(vOperatorResource.getResTarget());
						Set<String> inpSet = new HashSet<String>(Arrays.asList(invocationKey[0].toString().split(",")));
						Set<String> resSet = new HashSet<String>(Arrays.asList(vOperatorResource.getResTerm().split(",")));
						for (String val : inpSet) {
							if (resSet.contains(val)) {
								sb.append(val + ",");
							}
						}
						if (StringUtil.isEmpty(sb.toString())) {
							for (String val : resSet) {
								sb.append(val + ",");
							}
						}
						parameters.put(vOperatorResource.getResTarget(), new Object[] { sb.toString() });
					}
				}
			}
		}
		return invocation.invoke();
	}

}
